package org.esk.dablog.web.forms;

import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;
import org.springframework.validation.BindException;
import org.springframework.beans.factory.InitializingBean;
import org.esk.dablog.model.Entry;
import org.esk.dablog.model.Author;
import org.esk.dablog.model.entries.Ride;
import org.esk.dablog.service.EntryManager;
import org.esk.dablog.service.UserManager;
import org.esk.dablog.exceptions.ParameterException;
import org.esk.dablog.exceptions.BusinessException;
import org.esk.dablog.web.util.RequestUtils;
import org.esk.dablog.web.functions.EntryFunctions;
import org.acegisecurity.annotation.Secured;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * This class
 * User: jc
 * Date: 22.11.2006
 * Time: 12:32:34
 * $Id:$
 */
@Secured({"ROLE_REGISTERED_USER"})
public class AddEntryForm extends SimpleFormController implements InitializingBean {

    private EntryManager entryManager;
    private UserManager authorManager;
    private boolean safeInput = true;

    public AddEntryForm() {
        setCommandClass(Entry.class);
    }

    protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse httpServletResponse, Object object, BindException bindException) throws Exception {
        Entry e = (Entry) object;
        e.setIp(request.getRemoteAddr());
        Date d = RequestUtils.toDateParameter(request);
        if (d == null) {
            d = new Date();
        }
        e.setDate(d);
        String userName = request.getUserPrincipal().getName();
        Author a = getAuthorManager().getAuthorByUsername(userName);
        e.setAuthor(a);
        processEntry(request, e);
        if (safeInput) {
            e.setText(EntryFunctions.toBr(e.getText()));
        }
        e.setId(save(e));
        return new ModelAndView(new RedirectView(getSuccessView() + "/" + e.getId(), true));
    }

    /**
     * Default save the entry instance. May be overridden
     * @param e
     */
    protected long save(Entry e) throws BusinessException {
        return entryManager.addEntry(e);
    }


    /**
     * additionally processes before saving
     * @param e
     */
    protected void processEntry(HttpServletRequest httpServletRequest, Entry e) throws Exception {

    }

    protected Object formBackingObject(HttpServletRequest httpServletRequest) throws Exception {
        return getCommandClass().newInstance();
    }


    public void afterPropertiesSet() throws Exception {
        if (entryManager == null) {
            throw new IllegalStateException("AddEntryForm.entryManager should be initialized");
        }
        if (authorManager == null) {
            throw new IllegalStateException("AddEntryForm.authorManager should be initialized");
        }
    }


    public void setSafeInput(boolean safeInput) {
        this.safeInput = safeInput;
    }

    public EntryManager getEntryManager() {
        return entryManager;
    }

    public UserManager getAuthorManager() {
        return authorManager;
    }

    public void setEntryManager(EntryManager entryManager) {
        this.entryManager = entryManager;
    }

    public void setAuthorManager(UserManager authorManager) {
        this.authorManager = authorManager;
    }
}
